package com.szxy.datetest;

import org.junit.jupiter.api.Test;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

/**
 * @Author: zwz
 * @Date:2020/9/30 14:09
 * @Description:com.szxy.datetest
 * @Version:1.0
 **/
public class TestNewLocalDate {

    /**
     * 时区： ZoneId
     */
    @Test
    public void test7() {
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
//        availableZoneIds.forEach(System.out::println);

        LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println("now = " + now);

        LocalDateTime ldt2 = LocalDateTime.now();
        ZonedDateTime zonedDateTime = ldt2.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);

    }

    /**
     * 日期格式化
     */
    @Test
    public void test6() {
        LocalDateTime ldt1 = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
        String str = dtf.format(ldt1);
        System.out.println(str);
        System.out.println("=============================");
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年mm月dd日HH:mm:ss");
        String str2 = dtf2.format(ldt1);
        System.out.println(str2);
    }

    /**
     * 时间校正器
     */
    @Test
    public void test5() {
        LocalDate ld = LocalDate.now();
        System.out.println("ld = " + ld);
        // 时间校正器
        LocalDate ld2 = ld.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
        System.out.println(ld2);
        // 自定义校正器: 计算下一个工作日
        LocalDate localDate = LocalDate.of(2020, 10, 10);
        LocalDate ld3 = localDate.with((l) -> {
            LocalDate tld = (LocalDate) l;
            DayOfWeek dayOfWeek = tld.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                return tld.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                return tld.plusDays(2);
            } else {
                return tld.plusDays(1);
            }
        });
        System.out.println("ld3 = " + ld3);
    }


    // 4. Period： 计算两个“日期”之间的间隔
    @Test
    public void test4() {
        LocalDate ld1 = LocalDate.of(2010, 1, 3);
        LocalDate ld2 = LocalDate.now();
        Period period = Period.between(ld1, ld2);
        System.out.println(period.getYears() + "年");
        System.out.println(period.getMonths() + "月");
        System.out.println(period.getDays() + "天");
    }

    // 3. Duration: 两个“时间“之间的间隔
    @Test
    public void test03() throws InterruptedException {
        Instant ist1 = Instant.now();
        Thread.sleep(1000);
        Instant ist2 = Instant.now();
        Duration duration = Duration.between(ist1, ist2);
        System.out.println(duration.toMillis() + "毫秒");
        System.out.println(duration.getSeconds()+ "秒");
        System.out.println("--------------------------------------");
        LocalDateTime ldt1 = LocalDateTime.now();
        Thread.sleep(1000);
        LocalDateTime ldt2 = LocalDateTime.now();
        Duration duration2 = Duration.between(ldt1, ldt2);
        System.out.println("duration2 = " + duration2.toMillis() + "毫秒");
    }

    // 2. Instant 时间戳 （计算机时间： Unix 元年： 1970年1月1日 00:00:00 开始到现在的毫秒值）
    @Test
    public void test02() {
        // 默认 UTC 时区
        Instant ist = Instant.now();
        System.out.println("ist = " + ist);
        //  更换时区
        OffsetDateTime odt = ist.atOffset(ZoneOffset.ofHours(8));
        System.out.println(odt);
        // 获取毫秒值
        long toEpochSecond = odt.toEpochSecond();
        System.out.println("toEpochSecond = " + toEpochSecond);
        // 设置毫秒值
        Instant ist3 = Instant.ofEpochMilli(10);
        System.out.println("ist3 = " + ist3);

    }

    // 1. LocalDate、LocalTime、 LocalDateTime 类的实例是不可变的对象
    @Test
    public void test01() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println("ldt = " + ldt);

        LocalDateTime ldt2 = LocalDateTime.of(2020, 9, 8, 12, 30, 0);
        System.out.println("ldt2 = " + ldt2);

        LocalDateTime ldt3 = ldt2.plusYears(2);
        System.out.println("ldt3 = " + ldt3);

        LocalDateTime ldt4 = ldt3.minusMonths(4);
        System.out.println("ldt4 = " + ldt4);

        int year = ldt2.getYear();
        Month month = ldt2.getMonth();
        DayOfWeek dayOfWeek = ldt2.getDayOfWeek();
        System.out.println(year + " " + month + " " + dayOfWeek);


    }

}
